package com.yc.baihe.servlets;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.yc.baihe.beans.BhUser;
import com.yc.baihe.beans.UserList;
import com.yc.baihe.biz.IUserListBiz;
import com.yc.baihe.biz.StoryBiz;
import com.yc.baihe.biz.impl.StoryBizImpl;
import com.yc.baihe.biz.impl.UserListBizImpl;
import com.yc.baihe.utils.SessionAttributeNames;
import com.yc.baihe.utils.UploadUtil;


import sun.misc.BASE64Decoder;


public class UploadServlet extends CommonServlet {
	private PrintWriter out;
	private HttpSession session;
	private static final long serialVersionUID = 1L;
	private UploadUtil uploadUtil=new UploadUtil();
	private StoryBiz storyBiz=new StoryBizImpl();
	private IUserListBiz userListBiz=new UserListBizImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op=request.getParameter("op");
		session=request.getSession();
		out=response.getWriter();
		if("storyupload".equals(op)){
			storyupload(request,response);
		}else if("uptouxiang".equals(op)){
			uptouxiang(request,response);
		}
		
	}


	private void uptouxiang(HttpServletRequest request, HttpServletResponse response) {
		try {
			
			PageContext context=JspFactory.getDefaultFactory().getPageContext(this,request,response,null,true,8192,true);
			
			Map<String,String> map=uploadUtil.update(context);
			BASE64Decoder base64=new BASE64Decoder(); 
			//64位解码  
			byte[] buffer=base64.decodeBuffer(map.get("imageData"));
			//写进文件  
			String filPath="upload/"+new Date().getTime()+""+new Random().nextInt(100000)+".jpg";
			String touxiang=filPath;
			filPath=this.getServletContext().getRealPath(filPath);
			
			FileOutputStream fos=new FileOutputStream(filPath); 
			System.out.println("图片的路径"+filPath);
			//更改当前登录用户的头像
			BhUser bh=(BhUser) session.getAttribute(SessionAttributeNames.CURRENTUSERSINFO);
			int buid=bh.getBuid();
			
			System.out.println("头像"+touxiang);
			
			userListBiz.UpdatePhoto(buid, touxiang);
			
			fos.write(buffer);  
			fos.flush();  
			fos.close();  
			fos=null;  
			out.print(1);
		} catch (Exception e) {
			e.printStackTrace();
			out.print(0);
		}
		out.flush();
	}


	private void storyupload(HttpServletRequest request, HttpServletResponse response) {
		BhUser uf = (BhUser)session.getAttribute(SessionAttributeNames.CURRENTUSERSINFO);  
		int buid=uf.getBuid(); //用户序列号
		PageContext context=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);
		try {
			SessionAttributeNames.PATH=request.getSession().getServletContext().getRealPath("upload");//上传的路径
			Map<String, String> map=uploadUtil.update(context);
			String photos=map.get("photo");
			out.print(storyBiz.updataPhotoByBoy_id(buid,photos));
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
